/*
 * @Date: 2020-10-26 12:00:51
 * @LastEditors: E'vils
 * @LastEditTime: 2021-03-11 16:15:44
 * @Description: 
 * @FilePath: /src/pages/importTable/importTable.js
 */
/* eslint-disable no-undef */
//index.js
//获取应用实例
const app = getApp();
// import {
//     serializePathQuery
// } from "../../utils/api/http";
import {
    getCaptcha,
    incourseLogin,
    IncourseInclass,
    classresult,
    schoollist,
} from "../../utils/api/course";
Page({
    data: {
        StatusBar: app.globalData.StatusBar,
        CustomBar: app.globalData.CustomBar,
        ImgUrl: app.globalData.ImgUrl,
        displayArea: app.globalData.displayArea,
        qqAdd: "17053613",
        universityList: [],
        schoolPage: 1,
        numList: [{
            name: "数据解析",
            status: "2",
            statusText: "进行中..."
        }, {
            name: "课表导入",
            status: "1", // 0为失败,1为等待中,2为进行中,5为完成
            statusText: "未开始"
        }, {
            name: "生成信息",
            status: "1",
            statusText: "未开始"
        }, ],
        wrongButton: null, // 错误按钮
        currentState: 0, //当前进行到哪一步
        inImport: false, // 是否导入中
        importDis: true, //导入按钮禁用
        importLoading: false, //导入按钮loading
        sectionModal: false, //选择学校底部弹框
        hasMore: true, // 有学校
        schoolName: null,
        schoolId: null,
        CaptchaImg: null,
        username: null,
        password: null,
        vccode: null
    },
    /**
     * 后退一页
     */
    BackPage() {
        wx.navigateBack({
            delta: 1,
        });
    },
    onLoad() {
        this.getSchool(this.data.schoolPage);
    },
    onShow() {},

    /**
     * 获取学校
     */
    getSchool(page) {
        if (!this.data.hasMore) {
            return;
        }
        schoollist({
            sort: 1,
            page
        }).then(v => {
            this.setData({
                universityList: [...this.data.universityList, ...v.data.data]
            });
            if (v.data.last_page == page) {
                this.setData({
                    hasMore: false,
                });
            } else {
                this.setData({
                    schoolPage: page + 1,
                });
            }
        });
    },
    /**
     * 打开学校弹窗
     */
    showModal() {
        console.log("object", );
        this.setData({
            sectionModal: true
        });
    },
    /**
     * 选择学校
     */
    chooseSchool(e) {
        let {
            id,
            name
        } = e.currentTarget.dataset;
        console.log(id,
            name);
        this.setData({
            schoolName: name,
            schoolId: id,
        });
    },
    /**
     * 关闭学校弹窗
     */
    hideModal() {
        this.setData({
            sectionModal: false
        });
    },
    scrollBottom() {
        this.getSchool(this.data.schoolPage);
    },
    /**
     * 查看验证码
     */
    viewCaptcha() {
        let {
            schoolId,
        } = this.data;
        if (!schoolId) {
            wx.showModal({
                title: "未选择学校",
                content: "请选择你的学校",
                showCancel: true,
                cancelText: "取消",
                cancelColor: "#000000",
                confirmText: "确定",
                confirmColor: "#3CC51F",
                success: (result) => {
                    if (result.confirm) {
                        this.setData({
                            sectionModal: true
                        });
                    }
                },
            });
            return;
        }
        getCaptcha({
            schoolId
        }).then(v => {
            console.log("v", v);
            this.setData({
                CaptchaImg: v.data.vccode
            });
        });
    },
    /**
     * 信息输入
     */
    infoInput(e) {
        let {
            username,
            password,
            vccode
        } = this.data;
        let name = e.currentTarget.dataset.name;
        let value = e.detail.value;
        this.setData({
            [name]: value
        });
        if (username &&
            password &&
            vccode) {
            this.setData({
                importDis: false
            });
        }
    },
    /**
     * 导入课表
     */
    importSchedule() {
        let {
            username,
            schoolId,
            password,
            vccode
        } = this.data;
        if (!schoolId) {
            wx.showModal({
                title: "未选择学校",
                content: "请选择你的学校",
                showCancel: true,
                cancelText: "取消",
                cancelColor: "#000000",
                confirmText: "确定",
                confirmColor: "#3CC51F",
                success: (result) => {
                    if (result.confirm) {
                        this.setData({
                            sectionModal: true
                        });
                        console.log("'object :>> '", result);
                    }
                },
            });
            return;
        }
        this.setData({
            importLoading: true
        });
        incourseLogin({
            username,
            password,
            schoolId,
            vccode
        }).then(v => {
            if (v.code) {
                this.setData({
                    inImport: true,
                    importLoading: false
                });
                this.importProcess();
            } else {
                wx.showToast({
                    title: v.data.msg || "账号/密码不正确,请重新输入",
                    icon: "none",
                    duration: 1500,
                    mask: false,
                    success: () => {
                        this.setData({
                            importLoading: false
                        });
                    },
                });
            }

        });
    },
    /**
     * 课表流程
     */
    importProcess() {
        let setList = (num, status, statusText) => {
            let {
                numList
            } = this.data;
            numList[num].status = status;
            numList[num].statusText = statusText;
            console.log("numList", numList);
            this.setData({
                numList
            });
        };
        setList(0, 2, "导入中...");
        classresult().then(v => {
            if (v.code) {
                setList(0, 5, "导入成功");
                setList(1, 2, "导入中...");
                setTimeout(() => {
                    setList(1, 5, "导入成功");
                    setList(2, 2, "导入中...");
                    wx.nextTick(() => {
                        setTimeout(() => {
                            IncourseInclass().then(e => {
                                if (e.code) {
                                    setList(2, 5, "导入成功");
                                    app.getInfo();
                                    this.setData({
                                        wrongButton: "返回"
                                    });
                                } else {
                                    setList(2, 0, "导入失败,请重新导入");
                                    this.setData({
                                        wrongButton: "重新导入"
                                    });
                                    return;
                                }
                            });
                        }, 1400);
                    });
                }, 1400);
            } else {
                setList(0, 0, "导入失败,请重新导入");
                this.setData({
                    wrongButton: "重新导入"
                });
                return;
            }
        });
    },
    /**
     * 导入错误按钮
     */
    errorButton() {
        let {
            wrongButton
        } = this.data;
        if (wrongButton == "返回") {
            wx.switchTab({
                url: "/pages/timetable/timetable",
            });
        } else {
            this.setData({
                inImport: false,
            });
        }
    },
});